<?
//查重复 unit
//如果传递过来的参数没有unit公司的话，就直接创建一个  “未命名企业”。用于和联系人关联。
//查重复的联系人
//联系人没有名字，只有联系方式，就创建一个未命名联系人




//因为是同域名多库的形式，所以要先确定号数据库之后，config才能正常使用
//需要再config之后再引入
session_start();




include_once("../config.php");

$author               = json_decode($_REQUEST['author'], true);
$_SESSION['database'] = $_unit[$author['athloncode']]['database'];


//$_SESSION['database'] ="athlon_crm";


//只在登录的时候需要再引入一次。登录之后就会产生session，会在config中引入
include_once (ROOT_PATH_."/connection.php");


?>
<?php
//把留言内容生成联系数据 member_contact_log
$unit               = json_decode($_REQUEST['unit'], true);
$member             = json_decode($_REQUEST['member'], true);
$member_contact_log = json_decode($_REQUEST['member_contact_log'], true);


//相同电话的情况下，会否自动关联同一个人。
// 同一个ip的情况下，判断为同一个人

$member_arr = select_lines_all_items($table_Prefix . 'member',""," phone = '".$member['ai_phone']."' or ip = '".$member['ai_ip']."'",1);

if(count($member_arr) > 0){
    $unitId = $member_arr[0]['unitnid'];
    $memberId = $member_arr[0]['nid'];
}else{

    //保存公司
    if(!$unit['ai_name']){
        $unit['ai_name'] = '未命名企业';
    }
    if(!$member['ai_name']){
        $member['ai_name'] = "未命名联系人";
    }

    $table = $table_Prefix . 'unit';
    $sql=insert_date($table,$unit);
//echo $sql;
    $query = mysql_query($sql) or die ($sql);
    $unitId = mysql_insert_id($dblink);

//保存联系人
    $table = $table_Prefix . 'member';
    $member['ai_unitnid'] = $unitId;

    $member['ai_period'] = '每周';
    $member['ai_checkpoint'] = date("Y-m-d H:i:s",strtotime("+2 day"));
    $sql=insert_date($table,$member);
//echo $sql;
    $query = mysql_query($sql) or die ($sql);
    $memberId = mysql_insert_id($dblink);

}



//保存留言内容
$table                              = $table_Prefix . 'member_contact_log';
$member_contact_log["ai_unitnid"]   = $unitId;
$member_contact_log["ai_membernid"] = $memberId;
$member_contact_log["ai_name"]      = "系统生成";
$member_contact_log["ai_contact_way"]      = "网站留言";

$sql   = insert_date($table, $member_contact_log);
$query = mysql_query($sql) or die ($sql);









//创建用户后，应该开始通知业务员跟进

//获取设置表格的数据,查看通知模式
$setting=select_one_line_all_items($table_prefix."setting","nid > 0");


// 抢单，则通知所有人
if($setting['client_notice_mod']=="全体通知")
{
    $employee_list = select_lines_all_items($table_Prefix . "employee", "nid desc", "", "0");

    foreach($employee_list as $k=>$v)
    {
        $url="http://".$host."/api/wechat_message/notice_client_created.php?openid=".$v['openid'];

        if($v['openid'])
           {
            _sock($url);
           }
    }



}




//自动分配：按照比重分派
if($setting['client_notice_mod']=="权重分配")
{
    $employee_list = select_lines_all_items($table_Prefix . "employee", "nid desc", "", "0");


    //todo 这里应该根据设置的权重轮流派单。获取所有人的权重，然后随机数处理。
    //还要考虑设置的时候怎么设置。

        $url="http://".$host."/api/wechat_message/notice_client_created.php?openid=".$v['openid'];
       _sock($url);

}





//自动分配：按照比重分派
if($setting['client_notice_mod']=="轮流分配")
{
    //寻找nid大于上一次通知的人的nid
    $employee = select_one_line_all_items($table_Prefix . "employee", "nid > '".$setting['client_notice_lastest']."' and openid != '' ", "1");

    if($employee['nid'])
    {
        //如果有这个人，就发送信息
        $url="http://".$host."/api/wechat_message/notice_client_created.php?openid=".$employee['openid'];
        _sock($url);

        //print_r2($employee);


        //记录这次发送了信息给谁。更新新的client_notice_lastest
        $now_employee['ai_client_notice_lastest']=$employee['nid'];
        $sqlstr= update_date($table_prefix."setting",$now_employee,"nid","1");
        $query = mysql_query($sqlstr) or die ($sqlstr);

    }
    else
    {
        //如果上次的已经是一个轮回的最后一人，就重新查找nid最小的人
        $employee = select_one_line_all_items($table_Prefix . "employee", "nid > '0' and openid != '' ", "1");

        $url="http://".$host."/api/wechat_message/notice_client_created.php?openid=".$employee['openid'];
        _sock($url);

        //print_r2($employee);

        $now_employee['ai_client_notice_lastest']=$employee['nid'];

        //更新新的client_notice_lastest
        $sqlstr= update_date($table_prefix."setting",$now_employee,"nid","1");
        $query = mysql_query($sqlstr) or die ($sqlstr);
    }



}




//分配，则通知主管
if($setting['client_notice_mod']=="主管分配")
{

    $client_notice_manager= select_one_line_all_items($table_Prefix."employee","nid = '".$setting['client_notice_manager']."'");
    $url="http://".$host."/api/wechat_message/notice_client_created.php?openid=".$client_notice_manager['openid'];
    _sock($url);
}















//成功
echo 1;
?>
